package tsvetanv.inventory.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import tsvetanv.inventory.service.ArticleService;

/**
 * The <coe>ArticleController</code> class supports rendering the home page.
 * 
 * @author Tsvetan Vasilev
 * 
 */
public class ArticleController extends AbstractController {
	@Autowired
	private ArticleService articleService;

	private int FIVE_MINUTES = 5 * 60;

	// --- Getters,Setters,Constructors ----------------------------------------
	public ArticleController() {
		setSupportedMethods(new String[] { METHOD_GET });
		setCacheSeconds(FIVE_MINUTES);
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	// --- Methods -------------------------------------------------------------

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView("articleList");
		modelAndView.addObject("articles", articleService.getAllArticles());
		return modelAndView;
	}
}
